<?php

namespace app\admin\model\factor;

use app\admin\model\User;
use think\Model;


class Authority extends Model
{

    

    

    // 表名
    protected $name = 'carbon_factor_authority';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $type = [
      'checktime' => 'timestamp:Y-m-d H:i:s',
      'product_image' => 'array',
    ];
    // 追加属性
    protected $append = [
        'status_text',
    ];
    

    
    public function getStatusList()
    {
        return ['0' => '待审','1' => '已通过','2' => '驳回'];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function factor()
    {
        return $this->hasOne(CarbonFactor::class, 'id', 'factor_id');
    }

    public function userInfo()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function getQuantificationMethodList()
    {
        return ['1' => 'ISO 14067:2018 温室气体 产品碳足迹 量化要求和指南','2' => '产品碳足迹量化要求和指南 电动自行车 电动摩托车'];
    }

    public function getQuantificationMethodTextAttr($value, $data)
    {
        $value = $value ?: ($data['quantification_method'] ?? '');
        return $this->getQuantificationMethodList()[$value] ?? '';
    }

    public function getReportSn(): string
    {
        $no_use = true;
        $sn = '';
        while ($no_use) {
            $sn = get_random_str(3, 14);
            if (!$this->where('report_sn', $sn)->find()) {
                $no_use = false;
            }
        }
        return $sn;
    }
}
